<?php
 include 'inc/header.php';
?>

<?php
// 初始化表单数据和错误消息的变量
// 数据变量（$name, $email, $courseName, $content）
$name = $email = $courseName = $content = '';
// 错误消息变量（$nameErr, $emailErr, $courseNameErr, $contentErr)
$nameErr = $emailErr = $courseNameErr = $contentErr ='';

//表单提交
if(isset($_POST['submit'])){
    if(empty($nameErr) && empty($emailErr) && empty($courseNameErr) && empty($contentErr)){
        $sql = "INSERT INTO review (name,email,course_name,content) 
        VALUES('$name','$email','$courseName','$content')";
        if(mysqli_query($conn, $sql)){
            header('Location: reviews.php');
        }
        else{
            echo "出错:" . mysqli_error($conn);
        }
    }
    // if(empty($_POST['name'])){
    //     $nameErr = "姓名必须填!!!";
    // }
    // else{
    //     // FILTER_SANITIZE_FULL_SPECIAL_CHARS过滤器 会删除或编码特殊字符
    //     $name = filter_input(INPUT_POST,'name',FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    // }

    // if(empty($_POST['email'])){
    //     $emailErr = "邮箱必须填写!!!";
    // }
    // else{
    //     $email = filter_input(INPUT_POST,'email',FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    // }

    // if(empty($_POST['course'])){
    //     $emailErr = "课程必须填写!!!";
    // }
    // else{
    //     $email = filter_input(INPUT_POST,'course',FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    // }

    // if(empty($_POST['body'])){
    //     $emailErr = "评价必须填写!!!";
    // }
    // else{
    //     $email = filter_input(INPUT_POST,'body',FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    // }

}
// 通过简单的echo语句将姓名的错误信息（如果有的话）和用户输入的姓名（如果验证通过的话）输出到了网页上
    echo $nameErr;
    echo $name; 


    
?>


<img class="w-50 mb-3" src="img/1.jpg" alt="">
            <h2>课程评价</h2>
            <p class="lead text-center">
                为你所上的课程留下评价
            </p>
            <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" class="mt-4 w-75" method="POST">
                <div class="mb-3">
                    <label for="name" class="form-label">你的姓名</label> 
                    <!-- Bootstrap的is-invalid类 
                    当输入不满足验证条件（比如，字段为空）时，Bootstrap会以红色边框或其他错误提示的方式高亮显示该控件
                    -->
                    <input type="text " class="form-control <?php echo $nameErr ? 'is-invalid': null ; ?> " id="name" 
                    name="name" placeholder="请输入你的姓名">
                    <div class="invalid-feedback">
                        <?php echo $nameErr; ?>
                    </div>
                </div>

                <div class="mb-3">
                    <label for="email" class="form-label">你的邮箱</label> 
                    <input type="email " class="form-control <?php echo $emailErr ? 'is-invalid':null; ?> " id="email" 
                    name="email" placeholder="请输入你的邮箱">
                    <div class="invalid-feedback">
                        <?php echo $emailErr; ?>
                    </div>
                </div>

                <div class="mb-3">
                    <label for="course" class="form-label">课程名称</label> 
                    <input type="course " class="form-control <?php echo $courseNameErr ? 'is-invalid':null; ?>" id="course" 
                    name="course" placeholder="请输入你的课程名称">
                    <div class="invalid-feedback">
                        <?php echo $courseNameErr; ?>
                    </div>
                </div>

                <div class="mb-3">
                    <label for="body" class="form-label">评价内容</label> 
                    <textarea class="form-control <?php echo $contentErr ? 'is-invalid':null; ?>" id="body" 
                    name="body" rows="4" placeholder="请输入你的评价内容"></textarea>
                    <div class="invalid-feedback">
                        <?php echo $contentErr; ?>
                    </div>
                </div>

                <div class="mb-3">
                    <input type="submit" name="submit" value="提交评价" class="btn btn-dark w-100">
                </div>
            </form>
        
            <?php include 'inc/footer.php'; ?>